package com.example.port.Controller;

import com.example.port.dto.ApiResponse;
import com.example.port.entity.Invoice;
import com.example.port.entity.StorageFee;
import com.example.port.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/invoices")
public class InvoiceController {
    
    private final InvoiceService invoiceService;
    
    @Autowired
    public InvoiceController(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }
    
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ApiResponse<Integer> createInvoice(@RequestBody Invoice invoice) {
        int result = invoiceService.insertInvoice(invoice);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "创建发票失败");
    }
    
    @GetMapping
    public ApiResponse<List<Invoice>> getAllInvoices() {
        List<Invoice> invoices = invoiceService.getAllInvoices();
        return ApiResponse.success(invoices);
    }
    
    @GetMapping("/{id}")
    public ApiResponse<Invoice> getInvoiceById(@PathVariable Long id) {
        Invoice invoice = invoiceService.getInvoiceById(id);
        return ApiResponse.success(invoice);
    }
    
    @GetMapping("/{id}/fees")
    public ApiResponse<List<StorageFee>> getInvoiceFees(@PathVariable Long id) {
        System.out.println("获取发票费用明细，发票ID: " + id);
        List<StorageFee> fees = invoiceService.getInvoiceFees(id);
        System.out.println("找到费用数量: " + (fees != null ? fees.size() : 0));
        return ApiResponse.success(fees);
    }
    
    @PutMapping("/{id}/issue")
    public ApiResponse<Integer> issueInvoice(@PathVariable Long id) {
        int result = invoiceService.issueInvoice(id);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "开具发票失败");
    }
    
    @PostMapping("/generate-from-fees")
    @ResponseStatus(HttpStatus.CREATED)
    public ApiResponse<Integer> generateFromStorageFees(@RequestBody Map<String, Object> request) {
        @SuppressWarnings("unchecked")
        List<Long> feeIds = (List<Long>) request.get("feeIds");
        Integer clientId = (Integer) request.get("clientId");
        
        int result = invoiceService.generateInvoiceFromStorageFees(feeIds, clientId);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "从仓储费用生成发票失败");
    }
    
    @DeleteMapping("/{id}")
    public ApiResponse<Integer> deleteInvoice(@PathVariable Long id) {
        int result = invoiceService.deleteInvoice(id);
        if (result > 0) {
            return ApiResponse.success(result);
        }
        return ApiResponse.error(500, "删除发票失败");
    }
} 
